<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of entity_admin
 *
 * @author artcak
 */
class skim_model extends CI_model {
    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    /* Memasukkan Skim Baru */
    public function insert_skim($program_id,$skim_kode,$skim_nama,$skim_max_dana,$skim_max_anggota,$skim_panduan_path)
    {
        $this->db->where('PROGRAM_ID', $program_id);
        $this->db->where('PROGRAM_AKTIF', 1);
        $tmp1 = $this->db->get('program');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $data =  array(
            'PROGRAM_ID' => $program_id,
            'SKIM_KODE' => $skim_kode,
            'SKIM_NAMA' => $skim_nama,
            'SKIM_MAX_DANA' => $skim_max_dana,
            'SKIM_MAX_ANGGOTA' => $skim_max_anggota,
            'SKIM_PANDUAN_PATH' => $skim_panduan_path,
            'SKIM_AKTIF' => 1
        );
        $result = $this->db->insert('skim', $data);
        return $result;
    }
    
    /* Edit Skim */
    public function update_skim($skim_id, $program_id, $skim_kode, $skim_nama, $skim_max_dana, $skim_max_anggota)
    {
        $this->db->where('PROGRAM_ID', $program_id);
        $this->db->where('PROGRAM_AKTIF', 1);
        $tmp1 = $this->db->get('program');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }        
        $data = array(
            'PROGRAM_ID' => $program_id,
            'SKIM_KODE' => $skim_kode,
            'SKIM_NAMA' => $skim_nama,
            'SKIM_MAX_DANA' => $skim_max_dana,
            'SKIM_MAX_ANGGOTA' => $skim_max_anggota
        );        
        $this->db->where('skim_id',$skim_id);
        $result = $this->db->update('skim', $data);
        return $result;
    }
    
    public function insert_panduan_path($skim_id, $panduan_path)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
            'SKIM_PANDUAN_PATH' => $panduan_path
        );        
        $this->db->where('skim_id',$skim_id);
        $result = $this->db->update('skim', $data);
        return $result;        
    }
    
    /* menghapus skim */
    public function delete_skim($skim_id)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array('SKIM_AKTIF' => 0);
        $this->db->where('skim_id',$skim_id);
        $result = $this->db->update('skim',$data);
        return TRUE;
    }
    
    /* Menambahkan bidang penelitian pada skim */
    public function insert_bidang_penelitian_skim($skim_id,$bidang_pen_id)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('BIDANG_PEN_ID', $bidang_pen_id);
        $tmp1 = $this->db->get('bidang_penelitian');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('BIDANG_PEN_ID', $bidang_pen_id);
        $this->db->where('SKIM_ID', $skim_id);
        $tmp3 = $this->db->get('bidang_penelitian_skim');
        if($tmp3->num_rows() > 0)
        {
            return FALSE;
        }
        $data =  array(
            'BIDANG_PEN_ID' => $bidang_pen_id,
            'SKIM_ID' => $skim_id
        );
        $result = $this->db->insert('bidang_penelitian_skim', $data);
        return $result;        
    }
    
    /* Menghapus bidang penelitian pada skim */
    public function delete_bidang_penelitian_skim($skim_id,$bidang_pen_id)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('BIDANG_PEN_ID', $bidang_pen_id);
        $tmp1 = $this->db->get('bidang_penelitian');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('BIDANG_PEN_ID', $bidang_pen_id);
        $this->db->where('SKIM_ID', $skim_id);
        $tmp3 = $this->db->delete('bidang_penelitian_skim');
        return $tmp3;
    }
    
    /* Membuat Pengumuman Skim Baru */
    public function insert_pengumuman_skim_penelitian($skim_id,$pengumuman_judul,$pengumuman_deskripsi)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
          'SKIM_ID' => $skim_id,
          'PENGUMUMAN_JUDUL' => $pengumuman_judul,
          'PENGUMUMAN_DESKRIPSI' => $pengumuman_deskripsi
        );
        $result = $this->db->insert('pengumuman_skim_penelitian',$data);
        return $result;
    }
    /* Mengedit Pengumuman Skim */
    public function update_pengumuman_skim_penelitian($pengumuman_id,$pengumuman_judul,$pengumuman_deskripsi)
    {
        $this->db->where('PENGUMUMAN_ID', $pengumuman_id);
        $tmp1 = $this->db->get('pengumuman_skim_penelitian');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        
        $data = array(
          'PENGUMUMAN_JUDUL' => $pengumuman_judul,
          'PENGUMUMAN_DESKRIPSI' => $pengumuman_deskripsi
        );
        $this->db->where('PENGUMUMAN_ID', $pengumuman_id);
        $result = $this->db->update('pengumuman_skim_penelitian',data);
        return $result;
    }
    
    /* menghapus bidang penelitian skim */
    public function delete_pengumuman_skim_penelitian($pengumuman_id)
    {
        $this->db->where('PENGUMUMAN_ID', $pengumuman_id);
        $tmp1 = $this->db->get('pengumuman_skim_penelitian');
        if($tmp1->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('PENGUMUMAN_ID', $pengumuman_id);
        $this->db->delete('pengumuman_skim_penelitian');
    }
    
    public function get_pengumuman_skim_penelitian_by_skim_id($skim_id)
    {
        $this->db->where('SKIM_ID',$skim_id);
        return $this->db->get('pengumuman_skim_penelitian');
    }
    
    /* Menambah persyaratan skim penelitian */
    public function insert_persyaratan_skim($skim_id,$syarat_deskripsi)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
          'SKIM_ID' => $skim_id,
          'SYARAT_DESKRIPSI' => $syarat_deskripsi
        );
        $result = $this->db->insert('persyaratan_skim',$data);
        return $result;
    }
    
    /* Update Persyaratan Skim */
    public function update_persyaratan_skim($syarat_id,$syarat_deskripsi)
    {
        $this->db->where('SYARAT_ID', $syarat_id);
        $tmp2 = $this->db->get('persyaratan_skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }        
        $data = array(
          'SYARAT_DESKRIPSI' => $syarat_deskripsi
        );
        $result = $this->db->update('persyaratan_skim',$data);
        return $result;        
    }
    
    /* Hapus persyaratan skim */
    public function delete_persyaratan_skim($syarat_id)
    {
        $this->db->where('SYARAT_ID', $syarat_id);
        $tmp2 = $this->db->get('persyaratan_skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }      
        $this->db->where('SYARAT_ID', $syarat_id);
        $result = $this->db->delete('persyaratan_skim');
        return $result;
    }
    
    public function get_persyaratan_skim_by_skim_id($skim_id)
    {
        $this->db->where('SKIM_ID',$skim_id);
        return $this->db->get('persyaratan_skim');
    }
    
    /* Insert Jadwal Skim */
    public function insert_jadwal_skim($skim_id, $jadwal_nama,$jadwal_selesai,$jadwal_mulai)
    {
        $this->db->where('SKIM_ID', $skim_id);
        $this->db->where('SKIM_AKTIF',1);
        $tmp2 = $this->db->get('skim');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
          'SKIM_ID' => $skim_id,
          'JADWAL_NAMA' => $jadwal_nama,
          'JADWAL_SELESAI' => $jadwal_selesai,
          'JADWAL_MULAI' => $jadwal_mulai
        );
        $result = $this->db->insert('jadwal',$data);
        return $result;        
    }
    
    /* update jadwal skim */
    public function update_jadwal_skim($jadwal_id,$jadwal_nama, $jadwal_selesai, $jadwal_mulai)
    {
        $this->db->where('JADWAL_ID', $jadwal_id);
        $tmp2 = $this->db->get('jadwal');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $data = array(
          'JADWAL_NAMA' => $jadwal_nama,
          'JADWAL_SELESAI' => $jadwal_selesai,
          'JADWAL_MULAI' => $jadwal_mulai
        );
        $result = $this->db->update('jadwal',$data);
        return $result;         
    }
    
    /* delete jadwal skim */
    public function hapus_jadwal_skim($jadwal_id)
    {
        $this->db->where('JADWAL_ID', $jadwal_id);
        $tmp2 = $this->db->get('jadwal');
        if($tmp2->num_rows() <= 0)
        {
            return FALSE;
        }
        $this->db->where('JADWAL_ID', $jadwal_id);
        $result = $this->db->delete('jadwal');
        return $result;
    }
    
    public function get_program_bytahun($tahun_id){
        $this->db->where('TAHUN_ID',$tahun_id);
        $query = $this->db->get('PROGRAM');
        return $query;
    }


    public function get_skim()
    {
        $this->db->where('SKIM_AKTIF',1); 
        $query = $this->db->get('SKIM');
        return $query;
    }
    
    public function get_skim_from_id($skim_id)
    {
        $this->db->where('SKIM_AKTIF',1); 
        $this->db->where('SKIM_ID',$skim_id);
        $query = $this->db->get('skim');
        return $query;
    }
    
    public function get_skim_byprogram($program_id)
    {
        $this->db->where('SKIM_AKTIF',1); 
        $this->db->where('PROGRAM_ID', $program_id);
        $result = $this->db->get('skim');
        return $result;
    }
    
    public function insert_bidang_penelitian($bidang_pen_id,$bidang_pen_nama)
    {
        $this->db->where('BIDANG_PEN_ID',$bidang_pen_id);
        $this->db->or_where('BIDANG_PEN_NAMA',$bidang_pen_nama);
        $tmp = $this->db->get('bidang_penelitian');
        if($tmp->num_rows() > 0)
        {
            return FALSE;
        }
        $data = array
        (
          'BIDANG_PEN_ID' => $bidang_pen_id,
          'BIDANG_PEN_NAMA' => $bidang_pen_nama
        );
        $this->db->insert('bidang_penelitian',$data);
    }
    public function delete_bidang_penelitian($bidang_pen_id)
    {
        $this->db->where('BIDANG_PEN_ID',$bidang_pen_id);
        $this->db->delete('bidang_penelitian_skim');
        $this->db->where('BIDANG_PEN_ID',$bidang_pen_id);
        $this->db->delete('bidang_penelitian');
    }
    public function get_bidang_penelitian()
    {
       return $this->db->get('bidang_penelitian');
    }
    public function get_bidang_penelitian_by_id($bidang_pen_id)
    {
       $this->db->where('BIDANG_PEN_ID',$bidang_pen_id);
       return $this->db->get('bidang_penelitian');
    }    
    public function get_bidang_penelitian_skim($skim_id)
    {
        $this->db->where('SKIM_ID',$skim_id);
        return $this->db->get('bidang_penelitian_skim');
    }
}?>
